#ifndef _PROTOCOL_SERVER_H_
#define _PROTOCOL_SERVER_H_
#include "netTool.h"
#include "serial_common.h"
#include "eventControl.h"
#include "canbusTool.h"

#include <memory>
#include <atomic>
#include <sys/socket.h>




#define PROTOCOL_DATA_BUFFER_SIZE      4096
#define PROTOCOL_DATA_STATIC_LENGTH    40
#define PROTOCOL_CMD_STATIC_LENGTH     0x22

class protocolServer:public cmdWorker{
public:
    protocolServer();
    virtual ~protocolServer();
    // 可以同时接收串口与can总线发送过来的数据。但注意指令间不能存在逻辑冲突，否则不能保证系统仍能正常运行。
    virtual int protocalParse(int fd,const char* ,int ,std::string&) override;
    virtual std::string protocalSerial(const char* data,int length ) override { return std::string(data, length); }


    int serverInit();

    int sendTest();
private:
    int     sysStatReport(BoradStatus& stat);
    void    boardStatCheck(BoradStatus& stat);
    void    convertToEL(BoradStatus& stat,uint8_t* elMem);
    int     realParser(uint8_t* data,int length);
    int     cmdParser(ControlInfo& ctlInfo);
    void    ctlInfoSave(const uint8_t* data,ControlInfo& ctlInfo);
    

    // 图像处理结果，每处理完一帧更新
    int algorResultUpdate();

private:
//串口通信相关 
    int uartFD;
    std::unique_ptr<serialTool>  comTool;

//can总线通信相关
    int canFD;
    std::unique_ptr<canbusTool>  canTool;

    uint32_t    comDataIndex;
    uint8_t     bufComData[PROTOCOL_DATA_BUFFER_SIZE];

    uint32_t    canDataIndex;
    uint8_t     bufCanData[PROTOCOL_DATA_BUFFER_SIZE];

//  指令响应相关
    std::unique_ptr<ImgProcManager> procManager;
    std::shared_ptr<notifyCond<image_detect_info>> cond;
    std::unique_ptr<ThreadHelper>   hThrStatus;

//  发送给控制仓信息
    BoradStatus boardStat;
//  控制舱发送过来的消息
    ControlInfo ctlStaInfo;
    std::mutex  mutex_ctlInfo;
//  存储目标位置信息
    image_detect_info osd_rect;
};

#endif